/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.general.managed;

import java.util.List;
import javax.faces.bean.ManagedBean;
import sv.com.intesal.clases.abstracts.ViewScopedAbstract;
import sv.com.intesal.clases.others.FormularioName.GENERALES.Mtto;
import sv.com.intesal.general.bean.Banco;
import sv.com.intesal.general.controlador.BancoControlador;

/**
 *
 * @author ejerez
 */
@ManagedBean(name = "bancoManaged")
public class BancoManaged extends ViewScopedAbstract {

    private Banco selectedBanco;
    private List<Banco> bancos;

    public BancoManaged() {
        this.selectedBanco = new Banco();
        this.bancos = null;
    }

    public void fillGrid() {
        try {
            if (this.bancos == null) {
                if (sm.validar_rol_usuario(Mtto.wfrmBanco, sm.buscar) == true) {
                    BancoControlador ac = new BancoControlador();
                    this.bancos = ac.getBancos();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    public void completeTransaction() {
        try {
            if (this.validar() == true) {
                if (this.selectedBanco.getCodigo_b() == null) {
                    if (sm.validar_rol_usuario(Mtto.wfrmBanco, sm.agregar) == true) {
                        BancoControlador mc = new BancoControlador();
                        mc.insert(this.selectedBanco);
                        m.messageInfo(m.getIinformation());
                        lm.limpiar();
                    }
                } else {
                    if (sm.validar_rol_usuario(Mtto.wfrmBanco, sm.modificar) == true) {
                        BancoControlador mc = new BancoControlador();
                        mc.update(this.selectedBanco);
                        m.messageInfo(m.getImodificada());
                        lm.limpiar();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    private Boolean validar() throws Exception {
        try {
            Boolean var_return = true;

            if (this.selectedBanco.getNombre_b().equals("")) {
                var_return = false;
                m.messageAdvertencia(m.campoError("NOMBRE"));
            }

            return var_return;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    /*SETTERS AND GETTERS*/
    public List<Banco> getBancos() {
        return bancos;
    }

    public void setBancos(List<Banco> bancos) {
        this.bancos = bancos;
    }

    public Banco getSelectedBanco() {
        if (selectedBanco == null) {
            selectedBanco = new Banco();
        }
        return selectedBanco;
    }

    public void setSelectedBanco(Banco selectedBanco) {
        this.selectedBanco = selectedBanco;
    }
}
